using System;
using System.Data;
using System.Data.SQLite;

namespace WIT.Global;

/// <summary>
/// SQLite帮助类
/// </summary>
public sealed class SQLiteHelper
{
	/// <summary>
	/// this enum is used to indicate weather the connection was provided by the caller, or created by SQLiteHelper, so that
	/// we can set the appropriate CommandBehavior when calling ExecuteReader()
	/// </summary>
	private enum SQLiteConnectionOwnership
	{
		/// <summary>Connection is owned and managed by SQLiteHelper</summary>
		Internal,
		/// <summary>Connection is owned and managed by the caller</summary>
		External
	}

	private static string _ConnectionString = string.Empty;

	/// <summary>
	/// 连接字符串
	/// </summary>
	public static string ConnectionString
	{
		get
		{
			return _ConnectionString;
		}
		set
		{
			_ConnectionString = value;
		}
	}

	private SQLiteHelper()
	{
	}

	/// <summary>
	/// This method is used to attach array's of SQLiteParameters to an SQLiteCommand.
	///
	/// This method will assign a value of DbNull to any parameter with a direction of
	/// InputOutput and a value of null.  
	///
	/// This behavior will prevent default values from being used, but
	/// this will be the less common case than an intended pure output parameter (derived as InputOutput)
	/// where the user provided no input value.
	/// </summary>
	/// <param name="command">The command to which the parameters will be added</param>
	/// <param name="commandParameters">an array of SQLiteParameters tho be added to command</param>
	private static void AttachParameters(SQLiteCommand command, SQLiteParameter[] commandParameters)
	{
		foreach (SQLiteParameter sQLiteParameter in commandParameters)
		{
			if (sQLiteParameter.Direction == ParameterDirection.InputOutput && sQLiteParameter.Value == null)
			{
				sQLiteParameter.Value = DBNull.Value;
			}
			command.Parameters.Add(sQLiteParameter);
		}
	}

	/// <summary>
	/// This method assigns an array of values to an array of SQLiteParameters.
	/// </summary>
	/// <param name="commandParameters">array of SQLiteParameters to be assigned values</param>
	/// <param name="parameterValues">array of objects holding the values to be assigned</param>
	private static void AssignParameterValues(SQLiteParameter[] commandParameters, object[] parameterValues)
	{
		if (commandParameters != null && parameterValues != null)
		{
			if (commandParameters.Length != parameterValues.Length)
			{
				throw new ArgumentException("Parameter count does not match Parameter Value count.");
			}
			int i = 0;
			for (int num = commandParameters.Length; i < num; i++)
			{
				commandParameters[i].Value = parameterValues[i];
			}
		}
	}

	/// <summary>
	/// This method opens (if necessary) and assigns a connection, transaction, command type and parameters 
	/// to the provided command.
	/// </summary>
	/// <param name="command">the SQLiteCommand to be prepared</param>
	/// <param name="connection">a valid SQLiteConnection, on which to execute this command</param>
	/// <param name="transaction">a valid SQLiteTransaction, or 'null'</param>
	/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
	/// <param name="commandText">the stored procedure name or PL/SQL command</param> 
	/// <param name="commandParameters">an array of SQLiteParameters to be associated with the command or 'null' if no parameters are required</param>
	private static void PrepareCommand(SQLiteCommand command, SQLiteConnection connection, SQLiteTransaction transaction, CommandType commandType, string commandText, SQLiteParameter[] commandParameters)
	{
		if (connection.State != ConnectionState.Open)
		{
			connection.Open();
		}
		command.Connection = connection;
		command.CommandText = commandText;
		if (transaction != null)
		{
			command.Transaction = transaction;
		}
		command.CommandType = commandType;
		if (commandParameters != null)
		{
			AttachParameters(command, commandParameters);
		}
	}

	/// <summary>
	/// Execute an SQLiteCommand (that returns no resultset and takes no parameters) against the database specified in 
	/// the connection string. 
	/// </summary>
	/// <remarks>
	/// e.g.:  
	///  int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders");
	/// </remarks>
	/// <param name="connectionString">a valid connection string for an SQLiteConnection</param>
	/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
	/// <param name="commandText">the stored procedure name or PL/SQL command</param>  
	/// <returns>an int representing the number of rows affected by the command</returns>
	public static int ExecuteNonQuery(string connectionString, CommandType commandType, string commandText)
	{
		return ExecuteNonQuery(connectionString, commandType, commandText, (SQLiteParameter[])null);
	}

	/// <summary>
	/// Execute an SQLiteCommand (that returns no resultset) against the database specified in the connection string 
	/// using the provided parameters.
	/// </summary>
	/// <remarks>
	/// e.g.:  
	///  int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new SQLiteParameter("@prodid", 24));
	/// </remarks>
	/// <param name="connectionString">a valid connection string for a SQLiteConnection</param>
	/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
	/// <param name="commandText">the stored procedure name or PL/SQL command</param>  
	/// <param name="commandParameters">an array of SQLiteParameters used to execute the command</param>
	/// <returns>an int representing the number of rows affected by the command</returns>
	public static int ExecuteNonQuery(string connectionString, CommandType commandType, string commandText, params SQLiteParameter[] commandParameters)
	{
		using SQLiteConnection sQLiteConnection = new SQLiteConnection(connectionString);
		sQLiteConnection.Open();
		return ExecuteNonQuery(sQLiteConnection, commandType, commandText, commandParameters);
	}

	/// <summary>
	/// Execute a stored procedure via an SQLiteCommand (that returns no resultset) against the database specified in 
	/// the connection string using the provided parameter values.  This method will query the database to discover the parameters for the 
	/// stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.
	/// </summary>
	/// <remarks>
	/// This method provides no access to output parameters or the stored procedure's return value parameter.
	///
	/// e.g.:  
	///  int result = ExecuteNonQuery(connString, "PublishOrders", 24, 36);
	/// </remarks>
	/// <param name="connectionString">a valid connection string for a SQLiteConnection</param>
	/// <param name="spName">the name of the stored prcedure</param>
	/// <param name="parameterValues">an array of objects to be assigned as the input values of the stored procedure</param>
	/// <returns>an int representing the number of rows affected by the command</returns>
	public static int ExecuteNonQuery(string connectionString, string spName, params object[] parameterValues)
	{
		if (parameterValues != null && parameterValues.Length != 0)
		{
			SQLiteParameter[] spParameterSet = SQLiteHelperParameterCache.GetSpParameterSet(connectionString, spName);
			AssignParameterValues(spParameterSet, parameterValues);
			return ExecuteNonQuery(connectionString, CommandType.StoredProcedure, spName, spParameterSet);
		}
		return ExecuteNonQuery(connectionString, CommandType.StoredProcedure, spName);
	}

	/// <summary>
	/// Execute an SQLiteDbCommand (that returns no resultset and takes no parameters) against the provided SQLiteConnection. 
	/// </summary>
	/// <remarks>
	/// e.g.:  
	///  int result = ExecuteNonQuery(conn, CommandType.StoredProcedure, "PublishOrders");
	/// </remarks>
	/// <param name="connection">a valid SQLiteConnection</param>
	/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
	/// <param name="commandText">the stored procedure name or PL/SQL command</param>
	/// <returns>an int representing the number of rows affected by the command</returns>
	public static int ExecuteNonQuery(SQLiteConnection connection, CommandType commandType, string commandText)
	{
		return ExecuteNonQuery(connection, commandType, commandText, (SQLiteParameter[])null);
	}

	/// <summary>
	/// Execute an SQLiteCommand (that returns no resultset) against the specified SQLiteConnection 
	/// using the provided parameters.
	/// </summary>
	/// <remarks>
	/// e.g.:  
	///  int result = ExecuteNonQuery(conn, CommandType.StoredProcedure, "PublishOrders", new SQLiteParameter("@prodid", 24));
	/// </remarks>
	/// <param name="connection">a valid SQLiteConnection</param>
	/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
	/// <param name="commandText">the stored procedure name or PL/SQL command</param>  
	/// <param name="commandParameters">an array of SQLiteParamters used to execute the command</param>
	/// <returns>an int representing the number of rows affected by the command</returns>
	public static int ExecuteNonQuery(SQLiteConnection connection, CommandType commandType, string commandText, params SQLiteParameter[] commandParameters)
	{
		SQLiteCommand sQLiteCommand = new SQLiteCommand();
		PrepareCommand(sQLiteCommand, connection, null, commandType, commandText, commandParameters);
		return sQLiteCommand.ExecuteNonQuery();
	}

	/// <summary>
	/// Execute a stored procedure via an SQLiteCommand (that returns no resultset) against the specified SQLiteConnection 
	/// using the provided parameter values.  This method will query the database to discover the parameters for the 
	/// stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.
	/// </summary>
	/// <remarks>
	/// This method provides no access to output parameters or the stored procedure's return value parameter.
	///
	/// e.g.:  
	///  int result = ExecuteNonQuery(conn, "PublishOrders", 24, 36);
	/// </remarks>
	/// <param name="connection">a valid SQLiteConnection</param>
	/// <param name="spName">the name of the stored prcedure</param>
	/// <param name="parameterValues">an array of objects to be assigned as the input values of the stored procedure</param>
	/// <returns>an int representing the number of rows affected by the command</returns>
	public static int ExecuteNonQuery(SQLiteConnection connection, string spName, params object[] parameterValues)
	{
		if (parameterValues != null && parameterValues.Length != 0)
		{
			SQLiteParameter[] spParameterSet = SQLiteHelperParameterCache.GetSpParameterSet(connection.ConnectionString, spName);
			AssignParameterValues(spParameterSet, parameterValues);
			return ExecuteNonQuery(connection, CommandType.StoredProcedure, spName, spParameterSet);
		}
		return ExecuteNonQuery(connection, CommandType.StoredProcedure, spName);
	}

	/// <summary>
	/// Execute an SQLiteCommand (that returns no resultset and takes no parameters) against the provided SQLiteTransaction. 
	/// </summary>
	/// <remarks>
	/// e.g.:  
	///  int result = ExecuteNonQuery(trans, CommandType.StoredProcedure, "PublishOrders");
	/// </remarks>
	/// <param name="transaction">a valid SQLiteTransaction</param>
	/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
	/// <param name="commandText">the stored procedure name or PL/SQL command</param>  
	/// <returns>an int representing the number of rows affected by the command</returns>
	public static int ExecuteNonQuery(SQLiteTransaction transaction, CommandType commandType, string commandText)
	{
		return ExecuteNonQuery(transaction, commandType, commandText, (SQLiteParameter[])null);
	}

	/// <summary>
	/// Execute an SQLiteCommand (that returns no resultset) against the specified SQLiteTransaction
	/// using the provided parameters.
	/// </summary>
	/// <remarks>
	/// e.g.:  
	///  int result = ExecuteNonQuery(trans, CommandType.StoredProcedure, "GetOrders", new SQLiteParameter("@prodid", 24));
	/// </remarks>
	/// <param name="transaction">a valid SQLiteTransaction</param>
	/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
	/// <param name="commandText">the stored procedure name or PL/SQL command</param>  
	/// <param name="commandParameters">an array of SQLiteParamters used to execute the command</param>
	/// <returns>an int representing the number of rows affected by the command</returns>
	public static int ExecuteNonQuery(SQLiteTransaction transaction, CommandType commandType, string commandText, params SQLiteParameter[] commandParameters)
	{
		SQLiteCommand sQLiteCommand = new SQLiteCommand();
		PrepareCommand(sQLiteCommand, transaction.Connection, transaction, commandType, commandText, commandParameters);
		return sQLiteCommand.ExecuteNonQuery();
	}

	/// <summary>
	/// Execute a stored procedure via an SQLiteCommand (that returns no resultset) against the specified 
	/// SQLiteTransaction using the provided parameter values.  This method will query the database to discover the parameters for the 
	/// stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.
	/// </summary>
	/// <remarks>
	/// This method provides no access to output parameters or the stored procedure's return value parameter.
	///
	/// e.g.:  
	///  int result = ExecuteNonQuery(conn, trans, "PublishOrders", 24, 36);
	/// </remarks>
	/// <param name="transaction">a valid SQLiteTransaction</param>
	/// <param name="spName">the name of the stored procedure</param>
	/// <param name="parameterValues">an array of objects to be assigned as the input values of the stored procedure</param>
	/// <returns>an int representing the number of rows affected by the command</returns>
	public static int ExecuteNonQuery(SQLiteTransaction transaction, string spName, params object[] parameterValues)
	{
		if (parameterValues != null && parameterValues.Length != 0)
		{
			SQLiteParameter[] spParameterSet = SQLiteHelperParameterCache.GetSpParameterSet(transaction.Connection.ConnectionString, spName);
			AssignParameterValues(spParameterSet, parameterValues);
			return ExecuteNonQuery(transaction, CommandType.StoredProcedure, spName, spParameterSet);
		}
		return ExecuteNonQuery(transaction, CommandType.StoredProcedure, spName);
	}

	/// <summary>
	/// Execute an SQLiteCommand (that returns a resultset and takes no parameters) against the database specified in 
	/// the connection string. 
	/// </summary>
	/// <remarks>
	/// e.g.:  
	///  DataSet ds = ExecuteDataset(connString, CommandType.StoredProcedure, "GetOrders");
	/// </remarks>
	/// <param name="connectionString">a valid connection string for an SQLiteConnection</param>
	/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
	/// <param name="commandText">the stored procedure name or PL/SQL command</param>  
	/// <returns>a dataset containing the resultset generated by the command</returns>
	public static DataSet ExecuteDataset(string connectionString, CommandType commandType, string commandText)
	{
		return ExecuteDataset(connectionString, commandType, commandText, (SQLiteParameter[])null);
	}

	/// <summary>
	/// Execute an SQLiteCommand (that returns a resultset) against the database specified in the connection string 
	/// using the provided parameters.
	/// </summary>
	/// <remarks>
	/// e.g.:  
	///  DataSet ds = ExecuteDataset(connString, CommandType.StoredProcedure, "GetOrders", new SQLiteParameter("@prodid", 24));
	/// </remarks>
	/// <param name="connectionString">a valid connection string for an SQLiteConnection</param>
	/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
	/// <param name="commandText">the stored procedure name or PL/SQL command</param> 
	/// <param name="commandParameters">an array of SQLiteParamters used to execute the command</param>
	/// <returns>a dataset containing the resultset generated by the command</returns>
	public static DataSet ExecuteDataset(string connectionString, CommandType commandType, string commandText, params SQLiteParameter[] commandParameters)
	{
		using SQLiteConnection sQLiteConnection = new SQLiteConnection(connectionString);
		sQLiteConnection.Open();
		return ExecuteDataset(sQLiteConnection, commandType, commandText, commandParameters);
	}

	/// <summary>
	/// Execute a stored procedure via an SQLiteCommand (that returns a resultset) against the database specified in 
	/// the conneciton string using the provided parameter values.  This method will query the database to discover the parameters for the 
	/// stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.
	/// </summary>
	/// <remarks>
	/// This method provides no access to output parameters or the stored procedure's return value parameter.
	///
	/// e.g.:  
	///  DataSet ds = ExecuteDataset(connString, "GetOrders", 24, 36);
	/// </remarks>
	/// <param name="connectionString">a valid connection string for an SQLiteConnection</param>
	/// <param name="spName">the name of the stored procedure</param>
	/// <param name="parameterValues">an array of objects to be assigned as the input values of the stored procedure</param>
	/// <returns>a dataset containing the resultset generated by the command</returns>
	public static DataSet ExecuteDataset(string connectionString, string spName, params object[] parameterValues)
	{
		if (parameterValues != null && parameterValues.Length != 0)
		{
			SQLiteParameter[] spParameterSet = SQLiteHelperParameterCache.GetSpParameterSet(connectionString, spName);
			AssignParameterValues(spParameterSet, parameterValues);
			return ExecuteDataset(connectionString, CommandType.StoredProcedure, spName, spParameterSet);
		}
		return ExecuteDataset(connectionString, CommandType.StoredProcedure, spName);
	}

	/// <summary>
	/// Execute an SQLiteCommand (that returns a resultset and takes no parameters) against the provided SQLiteConnection. 
	/// </summary>
	/// <remarks>
	/// e.g.:  
	///  DataSet ds = ExecuteDataset(conn, CommandType.StoredProcedure, "GetOrders");
	/// </remarks>
	/// <param name="connection">a valid SQLiteConnection</param>
	/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
	/// <param name="commandText">the stored procedure name or PL/SQL command</param>
	/// <returns>a dataset containing the resultset generated by the command</returns>
	public static DataSet ExecuteDataset(SQLiteConnection connection, CommandType commandType, string commandText)
	{
		return ExecuteDataset(connection, commandType, commandText, (SQLiteParameter[])null);
	}

	/// <summary>
	/// Execute an SQLiteCommand (that returns a resultset) against the specified SQLiteConnection 
	/// using the provided parameters.
	/// </summary>
	/// <remarks>
	/// e.g.:  
	///  DataSet ds = ExecuteDataset(conn, CommandType.StoredProcedure, "GetOrders", new SQLiteParameter("@prodid", 24));
	/// </remarks>
	/// <param name="connection">a valid SQLiteConnection</param>
	/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
	/// <param name="commandText">the stored procedure name or PL/SQL command</param> 
	/// <param name="commandParameters">an array of SQLiteParamters used to execute the command</param>
	/// <returns>a dataset containing the resultset generated by the command</returns>
	public static DataSet ExecuteDataset(SQLiteConnection connection, CommandType commandType, string commandText, params SQLiteParameter[] commandParameters)
	{
		SQLiteCommand sQLiteCommand = new SQLiteCommand();
		PrepareCommand(sQLiteCommand, connection, null, commandType, commandText, commandParameters);
		SQLiteDataAdapter sQLiteDataAdapter = new SQLiteDataAdapter(sQLiteCommand);
		DataSet dataSet = new DataSet();
		sQLiteDataAdapter.Fill(dataSet);
		return dataSet;
	}

	/// <summary>
	/// Execute a stored procedure via an SQLiteCommand (that returns a resultset) against the specified SQLiteConnection 
	/// using the provided parameter values.  This method will query the database to discover the parameters for the 
	/// stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.
	/// </summary>
	/// <remarks>
	/// This method provides no access to output parameters or the stored procedure's return value parameter.
	///
	/// e.g.:  
	///  DataSet ds = ExecuteDataset(conn, "GetOrders", 24, 36);
	/// </remarks>
	/// <param name="connection">a valid SQLiteConnection</param>
	/// <param name="spName">the name of the stored prcedure</param>
	/// <param name="parameterValues">an array of objects to be assigned as the input values of the stored procedure</param>
	/// <returns>a dataset containing the resultset generated by the command</returns>
	public static DataSet ExecuteDataset(SQLiteConnection connection, string spName, params object[] parameterValues)
	{
		if (parameterValues != null && parameterValues.Length != 0)
		{
			SQLiteParameter[] spParameterSet = SQLiteHelperParameterCache.GetSpParameterSet(connection.ConnectionString, spName);
			AssignParameterValues(spParameterSet, parameterValues);
			return ExecuteDataset(connection, CommandType.StoredProcedure, spName, spParameterSet);
		}
		return ExecuteDataset(connection, CommandType.StoredProcedure, spName);
	}

	/// <summary>
	/// Execute an SQLiteCommand (that returns a resultset and takes no parameters) against the provided SQLiteTransaction. 
	/// </summary>
	/// <remarks>
	/// e.g.:  
	///  DataSet ds = ExecuteDataset(trans, CommandType.StoredProcedure, "GetOrders");
	/// </remarks>
	/// <param name="transaction">a valid SQLiteTransaction</param>
	/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
	/// <param name="commandText">the stored procedure name or PL/SQL command</param> 
	/// <returns>a dataset containing the resultset generated by the command</returns>
	public static DataSet ExecuteDataset(SQLiteTransaction transaction, CommandType commandType, string commandText)
	{
		return ExecuteDataset(transaction, commandType, commandText, (SQLiteParameter[])null);
	}

	/// <summary>
	/// Execute an SQLiteCommand (that returns a resultset) against the specified SQLiteTransaction
	/// using the provided parameters.
	/// </summary>
	/// <remarks>
	/// e.g.:  
	///  DataSet ds = ExecuteDataset(trans, CommandType.StoredProcedure, "GetOrders", new SQLiteParameter("@prodid", 24));
	/// </remarks>
	/// <param name="transaction">a valid SQLiteTransaction</param>
	/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
	/// <param name="commandText">the stored procedure name or PL/SQL command</param> 
	/// <param name="commandParameters">an array of SQLiteParamters used to execute the command</param>
	/// <returns>a dataset containing the resultset generated by the command</returns>
	public static DataSet ExecuteDataset(SQLiteTransaction transaction, CommandType commandType, string commandText, params SQLiteParameter[] commandParameters)
	{
		SQLiteCommand sQLiteCommand = new SQLiteCommand();
		PrepareCommand(sQLiteCommand, transaction.Connection, transaction, commandType, commandText, commandParameters);
		SQLiteDataAdapter sQLiteDataAdapter = new SQLiteDataAdapter(sQLiteCommand);
		DataSet dataSet = new DataSet();
		sQLiteDataAdapter.Fill(dataSet);
		return dataSet;
	}

	/// <summary>
	/// Execute a stored procedure via an SQLiteCommand (that returns a resultset) against the specified 
	/// SQLiteTransaction using the provided parameter values.  This method will query the database to discover the parameters for the 
	/// stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.
	/// </summary>
	/// <remarks>
	/// This method provides no access to output parameters or the stored procedure's return value parameter.
	///
	/// e.g.:  
	///  DataSet ds = ExecuteDataset(trans, "GetOrders", 24, 36);
	/// </remarks>
	/// <param name="transaction">a valid SQLiteTransaction</param>
	/// <param name="spName">the name of the stored prcedure</param>
	/// <param name="parameterValues">an array of objects to be assigned as the input values of the stored procedure</param>
	/// <returns>a dataset containing the resultset generated by the command</returns>
	public static DataSet ExecuteDataset(SQLiteTransaction transaction, string spName, params object[] parameterValues)
	{
		if (parameterValues != null && parameterValues.Length != 0)
		{
			SQLiteParameter[] spParameterSet = SQLiteHelperParameterCache.GetSpParameterSet(transaction.Connection.ConnectionString, spName);
			AssignParameterValues(spParameterSet, parameterValues);
			return ExecuteDataset(transaction, CommandType.StoredProcedure, spName, spParameterSet);
		}
		return ExecuteDataset(transaction, CommandType.StoredProcedure, spName);
	}

	/// <summary>
	/// Create and prepare an SQLiteCommand, and call ExecuteReader with the appropriate CommandBehavior.
	/// </summary>
	/// <remarks>
	/// If we created and opened the connection, we want the connection to be closed when the DataReader is closed.
	///
	/// If the caller provided the connection, we want to leave it to them to manage.
	/// </remarks>
	/// <param name="connection">a valid SQLiteConnection, on which to execute this command</param>
	/// <param name="transaction">a valid SQLiteTransaction, or 'null'</param>
	/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
	/// <param name="commandText">the stored procedure name or PL/SQL command</param> 
	/// <param name="commandParameters">an array of SQLiteParameters to be associated with the command or 'null' if no parameters are required</param>
	/// <param name="connectionOwnership">indicates whether the connection parameter was provided by the caller, or created by SQLiteHelper</param>
	/// <returns>SQLiteDataReader containing the results of the command</returns>
	private static SQLiteDataReader ExecuteReader(SQLiteConnection connection, SQLiteTransaction transaction, CommandType commandType, string commandText, SQLiteParameter[] commandParameters, SQLiteConnectionOwnership connectionOwnership)
	{
		SQLiteCommand sQLiteCommand = new SQLiteCommand();
		PrepareCommand(sQLiteCommand, connection, transaction, commandType, commandText, commandParameters);
		if (connectionOwnership == SQLiteConnectionOwnership.External)
		{
			return sQLiteCommand.ExecuteReader();
		}
		return sQLiteCommand.ExecuteReader(CommandBehavior.CloseConnection);
	}

	/// <summary>
	/// Execute an SQLiteCommand (that returns a resultset and takes no parameters) against the database specified in 
	/// the connection string. 
	/// </summary>
	/// <remarks>
	/// e.g.:  
	///  SQLiteDataReader dr = ExecuteReader(connString, CommandType.StoredProcedure, "GetOrders");
	/// </remarks>
	/// <param name="connectionString">a valid connection string for an SQLiteConnection</param>
	/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
	/// <param name="commandText">the stored procedure name or PL/SQL command</param>  
	/// <returns>an SQLiteDataReader containing the resultset generated by the command</returns>
	public static SQLiteDataReader ExecuteReader(string connectionString, CommandType commandType, string commandText)
	{
		return ExecuteReader(connectionString, commandType, commandText, (SQLiteParameter[])null);
	}

	/// <summary>
	/// Execute an SQLiteCommand (that returns a resultset) against the database specified in the connection string 
	/// using the provided parameters.
	/// </summary>
	/// <remarks>
	/// e.g.:  
	///  SQLiteDataReader dr = ExecuteReader(connString, CommandType.StoredProcedure, "GetOrders", new SQLiteParameter("@prodid", 24));
	/// </remarks>
	/// <param name="connectionString">a valid connection string for an SQLiteConnection</param>
	/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
	/// <param name="commandText">the stored procedure name or PL/SQL command</param>  
	/// <param name="commandParameters">an array of SQLiteParameters used to execute the command</param>
	/// <returns>an SQLiteDataReader containing the resultset generated by the command</returns>
	public static SQLiteDataReader ExecuteReader(string connectionString, CommandType commandType, string commandText, params SQLiteParameter[] commandParameters)
	{
		SQLiteConnection sQLiteConnection = new SQLiteConnection(connectionString);
		sQLiteConnection.Open();
		try
		{
			return ExecuteReader(sQLiteConnection, null, commandType, commandText, commandParameters, SQLiteConnectionOwnership.Internal);
		}
		catch
		{
			sQLiteConnection.Close();
			throw;
		}
	}

	/// <summary>
	/// Execute a stored procedure via an SQLiteCommand (that returns a resultset) against the database specified in 
	/// the connection string using the provided parameter values.  This method will query the database to discover the parameters for the 
	/// stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.
	/// </summary>
	/// <remarks>
	/// This method provides no access to output parameters or the stored procedure's return value parameter.
	///
	/// e.g.:  
	///  SQLiteDataReader dr = ExecuteReader(connString, "GetOrders", 24, 36);
	/// </remarks>
	/// <param name="connectionString">a valid connection string for an SQLiteConnection</param>
	/// <param name="spName">the name of the stored prcedure</param>
	/// <param name="parameterValues">an array of objects to be assigned as the input values of the stored procedure</param>
	/// <returns>an SQLiteDataReader containing the resultset generated by the command</returns>
	public static SQLiteDataReader ExecuteReader(string connectionString, string spName, params object[] parameterValues)
	{
		if (parameterValues != null && parameterValues.Length != 0)
		{
			SQLiteParameter[] spParameterSet = SQLiteHelperParameterCache.GetSpParameterSet(connectionString, spName);
			AssignParameterValues(spParameterSet, parameterValues);
			return ExecuteReader(connectionString, CommandType.StoredProcedure, spName, spParameterSet);
		}
		return ExecuteReader(connectionString, CommandType.StoredProcedure, spName);
	}

	/// <summary>
	/// Execute an SQLiteCommand (that returns a resultset and takes no parameters) against the provided SQLiteConnection. 
	/// </summary>
	/// <remarks>
	/// e.g.:  
	///  SQLiteDataReader dr = ExecuteReader(conn, CommandType.StoredProcedure, "GetOrders");
	/// </remarks>
	/// <param name="connection">a valid SQLiteConnection</param>
	/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
	/// <param name="commandText">the stored procedure name or PL/SQL command</param>
	/// <returns>an SQLiteDataReader containing the resultset generated by the command</returns>
	public static SQLiteDataReader ExecuteReader(SQLiteConnection connection, CommandType commandType, string commandText)
	{
		return ExecuteReader(connection, commandType, commandText, (SQLiteParameter[])null);
	}

	/// <summary>
	/// Execute an SQLiteCommand (that returns a resultset) against the specified SQLiteConnection 
	/// using the provided parameters.
	/// </summary>
	/// <remarks>
	/// e.g.:  
	///  SQLiteDataReader dr = ExecuteReader(conn, CommandType.StoredProcedure, "GetOrders", new SQLiteParameter("@prodid", 24));
	/// </remarks>
	/// <param name="connection">a valid SQLiteConnection</param>
	/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
	/// <param name="commandText">the stored procedure name or PL/SQL command</param>  
	/// <param name="commandParameters">an array of SQLiteParamters used to execute the command</param>
	/// <returns>an SQLiteDataReader containing the resultset generated by the command</returns>
	public static SQLiteDataReader ExecuteReader(SQLiteConnection connection, CommandType commandType, string commandText, params SQLiteParameter[] commandParameters)
	{
		return ExecuteReader(connection, null, commandType, commandText, commandParameters, SQLiteConnectionOwnership.External);
	}

	/// <summary>
	/// Execute a stored procedure via an SQLiteCommand (that returns a resultset) against the specified SQLiteConnection 
	/// using the provided parameter values.  This method will query the database to discover the parameters for the 
	/// stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.
	/// </summary>
	/// <remarks>
	/// This method provides no access to output parameters or the stored procedure's return value parameter.
	///
	/// e.g.:  
	///  SQLiteDataReader dr = ExecuteReader(conn, "GetOrders", 24, 36);
	/// </remarks>
	/// <param name="connection">a valid SQLiteConnection</param>
	/// <param name="spName">the name of the stored procedure</param>
	/// <param name="parameterValues">an array of objects to be assigned as the input values of the stored procedure</param>
	/// <returns>an SQLiteDataReader containing the resultset generated by the command</returns>
	public static SQLiteDataReader ExecuteReader(SQLiteConnection connection, string spName, params object[] parameterValues)
	{
		if (parameterValues != null && parameterValues.Length != 0)
		{
			SQLiteParameter[] spParameterSet = SQLiteHelperParameterCache.GetSpParameterSet(connection.ConnectionString, spName);
			AssignParameterValues(spParameterSet, parameterValues);
			return ExecuteReader(connection, CommandType.StoredProcedure, spName, spParameterSet);
		}
		return ExecuteReader(connection, CommandType.StoredProcedure, spName);
	}

	/// <summary>
	/// Execute an SQLiteCommand (that returns a resultset and takes no parameters) against the provided SQLiteTransaction. 
	/// </summary>
	/// <remarks>
	/// e.g.:  
	///  SQLiteDataReader dr = ExecuteReader(trans, CommandType.StoredProcedure, "GetOrders");
	/// </remarks>
	/// <param name="transaction">a valid SQLiteTransaction</param>
	/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
	/// <param name="commandText">the stored procedure name or PL/SQL command</param>  
	/// <returns>an SQLiteDataReader containing the resultset generated by the command</returns>
	public static SQLiteDataReader ExecuteReader(SQLiteTransaction transaction, CommandType commandType, string commandText)
	{
		return ExecuteReader(transaction, commandType, commandText, (SQLiteParameter[])null);
	}

	/// <summary>
	/// Execute an SQLiteCommand (that returns a resultset) against the specified SQLiteTransaction
	/// using the provided parameters.
	/// </summary>
	/// <remarks>
	/// e.g.:  
	///   SQLiteDataReader dr = ExecuteReader(trans, CommandType.StoredProcedure, "GetOrders", new SQLiteParameter("@prodid", 24));
	/// </remarks>
	/// <param name="transaction">a valid SQLiteTransaction</param>
	/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
	/// <param name="commandText">the stored procedure name or PL/SQL command</param> 
	/// <param name="commandParameters">an array of SQLiteParameters used to execute the command</param>
	/// <returns>an SQLiteDataReader containing the resultset generated by the command</returns>
	public static SQLiteDataReader ExecuteReader(SQLiteTransaction transaction, CommandType commandType, string commandText, params SQLiteParameter[] commandParameters)
	{
		return ExecuteReader(transaction.Connection, transaction, commandType, commandText, commandParameters, SQLiteConnectionOwnership.External);
	}

	/// <summary>
	/// Execute a stored procedure via an SQLiteCommand (that returns a resultset) against the specified
	/// SQLiteTransaction using the provided parameter values.  This method will query the database to discover the parameters for the 
	/// stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.
	/// </summary>
	/// <remarks>
	/// This method provides no access to output parameters or the stored procedure's return value parameter.
	///
	/// e.g.:  
	///  SQLiteDataReader dr = ExecuteReader(trans, "GetOrders", 24, 36);
	/// </remarks>
	/// <param name="transaction">a valid SQLiteTransaction</param>
	/// <param name="spName">the name of the stored prcedure</param>
	/// <param name="parameterValues">an array of objects to be assigned as the input values of the stored procedure</param>
	/// <returns>an SQLiteDataReader containing the resultset generated by the command</returns>
	public static SQLiteDataReader ExecuteReader(SQLiteTransaction transaction, string spName, params object[] parameterValues)
	{
		if (parameterValues != null && parameterValues.Length != 0)
		{
			SQLiteParameter[] spParameterSet = SQLiteHelperParameterCache.GetSpParameterSet(transaction.Connection.ConnectionString, spName);
			AssignParameterValues(spParameterSet, parameterValues);
			return ExecuteReader(transaction, CommandType.StoredProcedure, spName, spParameterSet);
		}
		return ExecuteReader(transaction, CommandType.StoredProcedure, spName);
	}

	/// <summary>
	/// Execute a SQLiteCommand (that returns a 1x1 resultset and takes no parameters) against the database specified in 
	/// the connection string. 
	/// </summary>
	/// <remarks>
	/// e.g.:  
	///  int orderCount = (int)ExecuteScalar(connString, CommandType.StoredProcedure, "GetOrderCount");
	/// </remarks>
	/// <param name="connectionString">a valid connection string for a SQLiteConnection</param>
	/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
	/// <param name="commandText">the stored procedure name or T-SQLite command</param>
	/// <returns>an object containing the value in the 1x1 resultset generated by the command</returns>
	public static object ExecuteScalar(string connectionString, CommandType commandType, string commandText)
	{
		return ExecuteScalar(connectionString, commandType, commandText, (SQLiteParameter[])null);
	}

	/// <summary>
	/// Execute a SQLiteCommand (that returns a 1x1 resultset) against the database specified in the connection string 
	/// using the provided parameters.
	/// </summary>
	/// <remarks>
	/// e.g.:  
	///  int orderCount = (int)ExecuteScalar(connString, CommandType.StoredProcedure, "GetOrderCount", new SQLiteParameter("@prodid", 24));
	/// </remarks>
	/// <param name="connectionString">a valid connection string for a SQLiteConnection</param>
	/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
	/// <param name="commandText">the stored procedure name or T-SQLite command</param>
	/// <param name="commandParameters">an array of SQLiteParamters used to execute the command</param>
	/// <returns>an object containing the value in the 1x1 resultset generated by the command</returns>
	public static object ExecuteScalar(string connectionString, CommandType commandType, string commandText, params SQLiteParameter[] commandParameters)
	{
		using SQLiteConnection sQLiteConnection = new SQLiteConnection(connectionString);
		sQLiteConnection.Open();
		return ExecuteScalar(sQLiteConnection, commandType, commandText, commandParameters);
	}

	/// <summary>
	/// Execute a stored procedure via a SQLiteCommand (that returns a 1x1 resultset) against the database specified in 
	/// the conneciton string using the provided parameter values.  This method will query the database to discover the parameters for the 
	/// stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.
	/// </summary>
	/// <remarks>
	/// This method provides no access to output parameters or the stored procedure's return value parameter.
	///
	/// e.g.:  
	///  int orderCount = (int)ExecuteScalar(connString, "GetOrderCount", 24, 36);
	/// </remarks>
	/// <param name="connectionString">a valid connection string for a SQLiteConnection</param>
	/// <param name="spName">the name of the stored prcedure</param>
	/// <param name="parameterValues">an array of objects to be assigned as the input values of the stored procedure</param>
	/// <returns>an object containing the value in the 1x1 resultset generated by the command</returns>
	public static object ExecuteScalar(string connectionString, string spName, params object[] parameterValues)
	{
		if (parameterValues != null && parameterValues.Length != 0)
		{
			SQLiteParameter[] spParameterSet = SQLiteHelperParameterCache.GetSpParameterSet(connectionString, spName);
			AssignParameterValues(spParameterSet, parameterValues);
			return ExecuteScalar(connectionString, CommandType.StoredProcedure, spName, spParameterSet);
		}
		return ExecuteScalar(connectionString, CommandType.StoredProcedure, spName);
	}

	/// <summary>
	/// Execute a SQLiteCommand (that returns a 1x1 resultset and takes no parameters) against the provided SQLiteConnection. 
	/// </summary>
	/// <remarks>
	/// e.g.:  
	///  int orderCount = (int)ExecuteScalar(conn, CommandType.StoredProcedure, "GetOrderCount");
	/// </remarks>
	/// <param name="connection">a valid SQLiteConnection</param>
	/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
	/// <param name="commandText">the stored procedure name or T-SQLite command</param>
	/// <returns>an object containing the value in the 1x1 resultset generated by the command</returns>
	public static object ExecuteScalar(SQLiteConnection connection, CommandType commandType, string commandText)
	{
		return ExecuteScalar(connection, commandType, commandText, (SQLiteParameter[])null);
	}

	/// <summary>
	/// Execute a SQLiteCommand (that returns a 1x1 resultset) against the specified SQLiteConnection 
	/// using the provided parameters.
	/// </summary>
	/// <remarks>
	/// e.g.:  
	///  int orderCount = (int)ExecuteScalar(conn, CommandType.StoredProcedure, "GetOrderCount", new SQLiteParameter("@prodid", 24));
	/// </remarks>
	/// <param name="connection">a valid SQLiteConnection</param>
	/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
	/// <param name="commandText">the stored procedure name or T-SQLite command</param>
	/// <param name="commandParameters">an array of SQLiteParamters used to execute the command</param>
	/// <returns>an object containing the value in the 1x1 resultset generated by the command</returns>
	public static object ExecuteScalar(SQLiteConnection connection, CommandType commandType, string commandText, params SQLiteParameter[] commandParameters)
	{
		SQLiteCommand sQLiteCommand = new SQLiteCommand();
		PrepareCommand(sQLiteCommand, connection, null, commandType, commandText, commandParameters);
		return sQLiteCommand.ExecuteScalar();
	}

	/// <summary>
	/// Execute a stored procedure via a SQLiteCommand (that returns a 1x1 resultset) against the specified SQLiteConnection 
	/// using the provided parameter values.  This method will query the database to discover the parameters for the 
	/// stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.
	/// </summary>
	/// <remarks>
	/// This method provides no access to output parameters or the stored procedure's return value parameter.
	///
	/// e.g.:  
	///  int orderCount = (int)ExecuteScalar(conn, "GetOrderCount", 24, 36);
	/// </remarks>
	/// <param name="connection">a valid SQLiteConnection</param>
	/// <param name="spName">the name of the stored prcedure</param>
	/// <param name="parameterValues">an array of objects to be assigned as the input values of the stored procedure</param>
	/// <returns>an object containing the value in the 1x1 resultset generated by the command</returns>
	public static object ExecuteScalar(SQLiteConnection connection, string spName, params object[] parameterValues)
	{
		if (parameterValues != null && parameterValues.Length != 0)
		{
			SQLiteParameter[] spParameterSet = SQLiteHelperParameterCache.GetSpParameterSet(connection.ConnectionString, spName);
			AssignParameterValues(spParameterSet, parameterValues);
			return ExecuteScalar(connection, CommandType.StoredProcedure, spName, spParameterSet);
		}
		return ExecuteScalar(connection, CommandType.StoredProcedure, spName);
	}

	/// <summary>
	/// Execute a SQLiteCommand (that returns a 1x1 resultset and takes no parameters) against the provided SQLiteTransaction. 
	/// </summary>
	/// <remarks>
	/// e.g.:  
	///  int orderCount = (int)ExecuteScalar(trans, CommandType.StoredProcedure, "GetOrderCount");
	/// </remarks>
	/// <param name="transaction">a valid SQLiteTransaction</param>
	/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
	/// <param name="commandText">the stored procedure name or T-SQLite command</param>
	/// <returns>an object containing the value in the 1x1 resultset generated by the command</returns>
	public static object ExecuteScalar(SQLiteTransaction transaction, CommandType commandType, string commandText)
	{
		return ExecuteScalar(transaction, commandType, commandText, (SQLiteParameter[])null);
	}

	/// <summary>
	/// Execute a SQLiteCommand (that returns a 1x1 resultset) against the specified SQLiteTransaction
	/// using the provided parameters.
	/// </summary>
	/// <remarks>
	/// e.g.:  
	///  int orderCount = (int)ExecuteScalar(trans, CommandType.StoredProcedure, "GetOrderCount", new SQLiteParameter("@prodid", 24));
	/// </remarks>
	/// <param name="transaction">a valid SQLiteTransaction</param>
	/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
	/// <param name="commandText">the stored procedure name or T-SQLite command</param>
	/// <param name="commandParameters">an array of SQLiteParamters used to execute the command</param>
	/// <returns>an object containing the value in the 1x1 resultset generated by the command</returns>
	public static object ExecuteScalar(SQLiteTransaction transaction, CommandType commandType, string commandText, params SQLiteParameter[] commandParameters)
	{
		SQLiteCommand sQLiteCommand = new SQLiteCommand();
		PrepareCommand(sQLiteCommand, transaction.Connection, transaction, commandType, commandText, commandParameters);
		return sQLiteCommand.ExecuteScalar();
	}

	/// <summary>
	/// Execute a stored procedure via a SQLiteCommand (that returns a 1x1 resultset) against the specified
	/// SQLiteTransaction using the provided parameter values.  This method will query the database to discover the parameters for the 
	/// stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.
	/// </summary>
	/// <remarks>
	/// This method provides no access to output parameters or the stored procedure's return value parameter.
	///
	/// e.g.:  
	///  int orderCount = (int)ExecuteScalar(trans, "GetOrderCount", 24, 36);
	/// </remarks>
	/// <param name="transaction">a valid SQLiteTransaction</param>
	/// <param name="spName">the name of the stored prcedure</param>
	/// <param name="parameterValues">an array of objects to be assigned as the input values of the stored procedure</param>
	/// <returns>an object containing the value in the 1x1 resultset generated by the command</returns>
	public static object ExecuteScalar(SQLiteTransaction transaction, string spName, params object[] parameterValues)
	{
		if (parameterValues != null && parameterValues.Length != 0)
		{
			SQLiteParameter[] spParameterSet = SQLiteHelperParameterCache.GetSpParameterSet(transaction.Connection.ConnectionString, spName);
			AssignParameterValues(spParameterSet, parameterValues);
			return ExecuteScalar(transaction, CommandType.StoredProcedure, spName, spParameterSet);
		}
		return ExecuteScalar(transaction, CommandType.StoredProcedure, spName);
	}
}
